package ec.atreasure.com.ec.login;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Patterns;
import android.view.View;
import android.widget.Toast;

import java.util.WeakHashMap;

import ec.atreasure.com.core.fragments.AtreasureFragment;
import ec.atreasure.com.core.net.rx.RxRestClient;
import ec.atreasure.com.core.wechat.AtreasureWechat;
import ec.atreasure.com.core.wechat.callback.IWechatLoginInCallback;
import ec.atreasure.com.ec.R;
import ec.atreasure.com.ec.main.HomeFragment;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.blankj.utilcode.util.BarUtils.getStatusBarHeight;

/**
 * Description : 登录界面
 * Created by wangjin on 2019/6/29.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class LoginFragment extends AtreasureFragment implements View.OnClickListener {

    private TextInputEditText mEmail = null;
    private TextInputEditText mPassword = null;
    private ILoginListener mLoginListener = null;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = (Activity) context;
        if (activity instanceof ILoginListener) {
            mLoginListener = (ILoginListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.login_fragment;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mEmail = findViewById(R.id.edit_sign_in_email);
        mPassword = findViewById(R.id.edit_sign_in_password);
        findViewById(R.id.btn_sign_in).setOnClickListener(this);
        findViewById(R.id.tv_link_sign_up).setOnClickListener(this);
        findViewById(R.id.icon_sign_in_wechat).setOnClickListener(this);
        findViewById(R.id.tb_sign_in).setPadding(0, getStatusBarHeight(), 0, 0);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_sign_in) {
            startWithPop(new HomeFragment());
            //TODO 登录后台交互逻辑
//            if (checkForm()) {
//                WeakHashMap<String, Object> weakHashMap = new WeakHashMap<>();
//                RxRestClient.getRxRestClientBuilder()
//                        .withUrl("")
//                        .withParams(weakHashMap)
//                        .setLoader(getContext())
//                        .build()
//                        .post()
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Observer<String>() {
//                            @Override
//                            public void onSubscribe(Disposable d) {
//
//                            }
//
//                            @Override
//                            public void onNext(String s) {
//                                //TODO 登录成功后逻辑操作
//                                if (mLoginListener != null)
//                                    mLoginListener.onLoginInSuccess();
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//
//                            }
//
//                            @Override
//                            public void onComplete() {
//
//                            }
//                        });
//            }
        } else if (i == R.id.tv_link_sign_up) {
            toRegisterFragment();
        } else if (i == R.id.icon_sign_in_wechat) {
            loginByWX();
        }
    }

    /**
     * 跳转注册界面
     */
    private void toRegisterFragment() {
        getSupportDelegate().start(new RegisterFragment(), SINGLETASK);
    }

    /**
     * 微信登录
     */
    private void loginByWX() {
        AtreasureWechat.getInstance().onLoginSuccess(new IWechatLoginInCallback() {
            @Override
            public void onLoginInSuccess(String userInfo) {
                Toast.makeText(getContext(), userInfo, Toast.LENGTH_LONG).show();
            }
        }).loginIn();
    }

    /**
     * 校验输入表单
     *
     * @return
     */
    private boolean checkForm() {
        final String email = mEmail.getText().toString();
        final String password = mPassword.getText().toString();

        boolean isPass = true;

        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEmail.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mPassword.setError("请填写至少6位数密码");
            isPass = false;
        } else {
            mPassword.setError(null);
        }
        return isPass;
    }
}
